module.exports = {
  pageTemplateWrap: function ({
    pagechinesename,
    actionName,
    storeName,
    typesName,
    reducerName,
    componentName,
    className,
    issub
  }) {
    const pageTemplate = `//base
import Taro, {Component} from '@tarojs/taro'
import {View, Text} from '@tarojs/components'
//redux
import {connect} from '@tarojs/redux'
import {${actionName}} from './store/actions/${storeName}'
import {${typesName}} from "./store/types/${storeName}";
//less
import './index.less'
${
  issub == 2
    ? `//异步加载reducer
//componnet
import NetworkError from '../../../components/taro-ui/network-error'
//libs
import {showLoading, hideLoading} from '../../../crossplatform/apiservice/toast'
import ${storeName} from './store/reducers/${storeName}'
import {getStore} from '../../../store/index'
import {injectReducer} from '../../../store/reducerUtils'

const store = getStore()
injectReducer(store, {
	key: '${storeName}',
	reducer: ${storeName}
})`
    : `
//componnet
import NetworkError from '../../components/taro-ui/network-error'
//libs
import {showLoading, hideLoading} from '../../crossplatform/apiservice/toast'
`
}

const mapActiontoProps = (dispatch) => ({
	updateState(payload) {
		dispatch({
			type: ${typesName},
			payload: {
				...payload
			}
		})
	},
	dorequest() {
		if (!this.timer) {
			this.timer = null
		}
		clearTimeout(this.timer)
		this.timer = setTimeout(() => {
			showLoading({
				title: '玩命加载中'
			})
		}, 2000)
		return new Promise((resolve) => {
			let promise = ${actionName}()
			dispatch(promise)
			promise.payload.then((data) => {
				clearTimeout(this.timer)
				this.timer = null
				hideLoading()
				resolve(data)
			})
		})
	}
})

@connect(({${reducerName}}) => ({
	...${reducerName}
}), mapActiontoProps)

class ${componentName} extends Component {

	config = {
		navigationBarTitleText: '${pagechinesename}'
	}

	componentWillMount() {//onLoad
	}

	componentDidMount() {//onready
	}

	componentDidShow() {//onshow
	}

	componentDidHide() {//onhide
	}

	componentWillUnmount() {//onUnload
	}
	
	queryWrap = () => {
		this.props.dorequest()
	}
	
	onReload = () => {
		const sharepayload = this.$router.params
		this.queryWrap(sharepayload)
	}

	delay(time = 0) {
		return new Promise((resolve) => {
			setTimeout(() => {
				resolve()
			}, time)
		})
	}

	render() {
		return (
			<View className='${className}' onClick={this.queryWrap}>
				<NetworkError visible={this.props.networkerror} onReload={this.onReload}></NetworkError>
				<View className={\`${className}Wrap \${this.props.networkerror ? 'hide' : ''}\`}>
					<View><Text>{this.props.text || '点我'}</Text></View>
				</View>
			</View>
		)
	}
}

export default ${componentName}`

    const pagecssTemplate = `.${className}{
	.${className}Wrap {
		display:block;
	}
	.hide {
		display:none;
	}
}`

    // store里面的文件模版
    const actionsTemplate = `import { ${typesName} } from "../types/${storeName}";
import { createAction } from 'redux-actions'

const formatdata = (entry) => {
	return {
		text:'开始啪啪啪'
	}
}

export const ${actionName} = createAction(${typesName}, (payload) => {
	return new Promise(resolve => {
		setTimeout(() => {
			resolve({
				...formatdata()
			})
		},1000)
		// doRequesttest({...payload}).then(({data}) => {
		// 	const {networkerror, entry, status, message} = data
		// 	if (networkerror) {
		// 		resolve({
		// 			networkerror: true
		// 		})
		// 	} else {
		// 		if (status && entry) {
		// 			let dataformat = formatdata(entry)
		// 			resolve({
		// 				networkerror: false,
		// 				showEmpty:false,
		// 				...dataformat
		// 			})
		// 		} else {
		// 			resolve({
		// 				networkerror: false,
		// 				showEmpty:true
		// 			})
		// 		}
		//
		// 	}
		// })
	})
})`

    const reducersTemplate = `import { ${typesName} } from '../types/${storeName}'
import { handleActions } from 'redux-actions'

const Schema = {
	showEmpty: false,
	requestOverFirst: false,
	networkerror: false,
	breaklocation: false,
	havenoshopbyshare: false
}

export default handleActions({
	[${typesName}](state, action) {
		return {
			...state,
			...action.payload
		}
	}
}, Schema)`

    const typesTemplate = `export const ${typesName} = '${typesName}'`

    return {
      pagecssTemplate,
      pageTemplate,
      actionsTemplate,
      reducersTemplate,
      typesTemplate
    }
  }
}
